package org.jeecg.modules.dafen.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.dafen.model.CnDafenresult;
import org.jeecg.modules.dafen.service.CnDafenresultService;
import org.jeecg.modules.dafen.service.CnPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/dd")
public class CnDafenresultController {

    @Autowired
    private CnDafenresultService cnDafenresultService;

    @Autowired
    private CnPersonService cnPersonService;

    // 1. 保存打分结果
    @PostMapping("/saveDafenResult")
    private Result<?> saveDafenResult(@RequestBody CnDafenresult cnDafenresult) {
        cnDafenresult.setScoreTime(new Date());
        //判断是否已经打过分、
        LambdaQueryWrapper<CnDafenresult> cnDafenresultLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cnDafenresultLambdaQueryWrapper.eq(CnDafenresult::getScorer, cnDafenresult.getScorer());
        cnDafenresultLambdaQueryWrapper.eq(CnDafenresult::getRatee, cnDafenresult.getRatee());
        CnDafenresult one = cnDafenresultService.getOne(cnDafenresultLambdaQueryWrapper);
        if (one != null) {
            //更新
            LambdaUpdateWrapper<CnDafenresult> cnDafenresultLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            cnDafenresultLambdaUpdateWrapper.eq(CnDafenresult::getScorer, cnDafenresult.getScorer());
            cnDafenresultLambdaUpdateWrapper.eq(CnDafenresult::getRatee, cnDafenresult.getRatee());
            cnDafenresultLambdaUpdateWrapper.set(CnDafenresult::getReultArr, cnDafenresult.getReultArr());
            cnDafenresultLambdaUpdateWrapper.set(CnDafenresult::getTotal, cnDafenresult.getTotal());
            cnDafenresultLambdaUpdateWrapper.set(CnDafenresult::getScoreTime, new Date());

            cnDafenresultService.update(cnDafenresultLambdaUpdateWrapper);
//            cnDafenresultService.up
            return Result.ok("保存成功");
        }
        cnDafenresultService.save(cnDafenresult);
        return Result.ok("保存成功");
    }
    // 2. 查询打分结果
    // 3. 删除打分结果
    // 4. 修改打分结果
    // 5. 查询打分结果列表
    //获取打分人列表
    @GetMapping("/getPersonList")
    private Result<?> getPersonList() {
        return Result.ok(cnPersonService.list());
    }
    //根据id获取人员
    @GetMapping("/getPersonById")
    private Result<?> getPersonById(@RequestParam Integer id) {
        return Result.ok(cnPersonService.getById(id));
    }

    //根据打分人和被打分对象确定
    @GetMapping("/getDafenResult")
    private Result<?> getDafenResult(@RequestParam String scorer, @RequestParam String ratee) {
        LambdaQueryWrapper<CnDafenresult> cnDafenresultLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cnDafenresultLambdaQueryWrapper.eq(CnDafenresult::getScorer, scorer);
        cnDafenresultLambdaQueryWrapper.eq(CnDafenresult::getRatee, ratee);
        return Result.ok(cnDafenresultService.getOne(cnDafenresultLambdaQueryWrapper));
    }

}
